#include <vector>

using namespace std;

class Solution {
public:
    int nthSuperUglyNumber(int n, vector<int>& primes) {
        vector<int> ans(1, 1);
        vector<int> ptr(primes.size(), 0);
        vector<int> temp(primes.size());
        while (ans.size() < n) {
            int min_val = INT32_MAX;
            for (int i = 0; i < ptr.size(); i++) {
                temp[i] = ans[ptr[i]] * primes[i];
                min_val = min(min_val, temp[i]);
            }
            for (int i = 0; i < ptr.size(); i++) {
                if (temp[i] == min_val) {
                    ptr[i]++;
                }
            }
            ans.push_back(min_val);
        }
        return ans[n - 1];
    }
};